package com.mobile.framework.util;


import sun.net.idn.Punycode;

import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;

/**
 * 文件名称为：URLAvailability.java
 * <p/>
 * 文件功能简述： 描述一个URL或图片地址是否有效
 *
 * @author ChenTao
 * @time 2008-5-29 上午10:00:35
 * @copyright
 */
public class URLUtil {


    /**
     * 功能描述 : 检测当前URL是否可连接或是否有效,
     * 最多连接网络 5 次, 如果 5 次都不成功说明该地址不存在或视为无效地址.
     *
     * @param url 指定URL网络地址
     * @return String
     */
    public static boolean isConnect(String url) {

        System.out.println("start isConnect");
        URL urlStr;

        HttpURLConnection connection;

        int counts = 0;
        if (url == null || url.length() <= 0) {
            return false;
        }
        while (counts < 5) {
            System.out.println("start while");
            try {
                urlStr = new URL(url);
                connection = (HttpURLConnection) urlStr.openConnection();
                connection.setConnectTimeout(5000);
                connection.setReadTimeout(5000);
                if (connection.getResponseCode() == 200) {
                    //succ = connection.getURL().toString();
                    return true;
                }
                break;
            } catch (Exception ex) {
                counts++;
                continue;
            }
        }
        System.out.println("start isConnect");
        return false;
    }

    public static String encodeZNUrl(String url) throws ParseException {


        if (url == null || url.trim().equals("")) {
            return null;
        }


        String urls[] = url.trim().split("\\.");


        if (urls == null || urls.length == 0) {
            return null;
        }

        String xn = "xn--";
        String cUrl = "";
        for (int i = 0; i < urls.length; i++) {
            boolean bool = false;
            String s = urls[i];
            for (int j = 0; j < s.length(); j++) {
                if (bool = s.substring(j, j + 1).matches("[\\u4e00-\\u9fa5]+")) {
                    break;
                }
            }
            if (bool) {
                cUrl += i == 0 ? xn + Punycode.encode(new StringBuffer(s), null) : "." + xn + Punycode.encode(new StringBuffer(s), null);
            } else {
                cUrl += i == 0 ? s : "." + s;
            }
        }
        return cUrl;
        /*
        if(urlL.indexOf(cn) == (urlL.length()-3) ){
            urlL = urlL.replaceAll(cn,"");
            String www = "";
            if(urlL.indexOf(ww) == 0){
               www = "www.";
               urlL = urlL.replaceAll(ww,"");
            }
            String newURl = xn + Punycode.encode(new StringBuffer(urlL),null);
            return www + newURl + cn;
        } else if(urlL.indexOf(zg) == (urlL.length()-3)){
            urlL = urlL.replaceAll(zg,"");
            String www = "";
            if(urlL.indexOf(ww) == 0){
               www = "www.";
               urlL = urlL.replaceAll(ww,"");
            }
            String newURl = xn + Punycode.encode(new StringBuffer(urlL),null);
            return www + newURl + "." + xn + Punycode.encode(new StringBuffer("中国"),null);
        } else {
            return null;
        }
        */
    }

    public static void main(String[] arge) throws ParseException {


        System.out.println(URLUtil.encodeZNUrl("www.融资城.cn"));


        System.out.println(URLUtil.encodeZNUrl("融资城.cn"));


        System.out.println(URLUtil.encodeZNUrl("融资城.公司"));

        System.out.println(URLUtil.encodeZNUrl("..融资城."));


        //"xn--"
        //System.out.println(isConnect("http://www.352.cn"));
    }
}

